$(document).ready(function(e) {
    $("#birthDay").val("");
    $.ajax({
        url: 'MessageShow.do',
        method: 'post',
        contentType: 'application/json', // 这句不加出现415错误:Unsupported Media Type
        dataType:"json",
        success: function(data) {
            if(data!=null){
                $("#name").val(data.name);
                $("#nickName").val(data.nickname);
               // if( $("input:radio[name='sex']").val() ==data.sex){
               //     $("input:radio[value='data.sex']").attr("checked","checked");
               // }
                // $("input[name='sex']").val(data.sex);
                $("#birthDay").val(ChangeDateFormat(data.birthDay.toString()));
                $("#address").val(data.address);
                $("#phoneNumber").val(data.phoneNumber);
            }
        },
        error: function(data) {
            console.log("error...");
        }
    });
    $("#addOrModify").click(function (e) {
        var name = $("#name").val();
        var nickName = $("#nickName").val();
        // var sex = $("input[name='sex']").val();
        // var birthday = new Date($("#birthDay").val());
        var birthday = $("#birthDay").val();
        var address = $("#address").val();
        var phoneNumber = $("#phoneNumber").val();
        var radios=$("input:radio[name='sex']:checked").val();
        if (name == "" || name == null) {
            alert("请填写姓名");
            return;
        }
        if (birthDay == "" || birthDay == null) {
            alert("请填写生日");
            return;
        }
        if (address == "" || address == null) {
            alert("请填写地址");
            return;
        }
        if (phoneNumber == "" || phoneNumber == null) {
            alert("请填写号码");
            return;
        }
        if(!isMobile(phoneNumber)){
            alert("请输入正确的手机号");
            return;
        }

        if(radios==null){
            alert("请选择性别");
            return;
        }

        var modifyVo={};
        modifyVo['name']=name;
        modifyVo['nickname']=nickName;
        modifyVo['sex']=radios;
        modifyVo['birthDay']=birthday;
        modifyVo['address']=address;
        modifyVo['phoneNumber']=phoneNumber;
        $.ajax({
            url: 'modify.do',
            method: 'post',
            contentType: 'application/json',
            data: JSON.stringify(modifyVo), // 以json字符串方式传递
            success: function(data) {
                if(data == "success"){
                    url="toMessage";
                    window.location.href=url;
                }
            },
            error:function (data) {
                console.log("error...");
            }
        });
    })
    function isMobile(s){
        var reg =/^(13[0-9]|15[0-9]|15[0-9]|17[0-9]|18[0-8])[0-9]{8}$/;
        var check=false;
        if (reg.test(s.trim()))
            check=true;
        return check;
    }

    function checkLength(valiCode,length){
        var len = valiCode.trim().length;
        if(len > length){
            return false;
        }
        return true;
    }
    function ChangeDateFormat(cellval) {
        var date = new Date(parseInt(cellval.replace("/Date(", "").replace(")/", ""), 10));
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        return date.getFullYear() + "-" + month + "-" + currentDate ;
    }

     function DateToUnix(birthDay) {
        var f = birthDay.split(' ', 2);
        var d = (f[0] ? f[0] : '').split('-', 3);
        var t = (f[1] ? f[1] : '').split(':', 3);
        return (new Date(
                parseInt(d[0], 10) || null,
                (parseInt(d[1], 10) || 1) - 1,
                parseInt(d[2], 10) || null,
                parseInt(t[0], 10) || null,
                parseInt(t[1], 10) || null,
                parseInt(t[2], 10) || null
            )).getTime() / 1000;
    }

    $("#close").click(function() {
        location.href = "toMessage.do";
    })
})
